package cn.edu.sdjzu.xg.kcsj.controller.authority;

import cn.edu.sdjzu.xg.kcsj.security.Resource;
import cn.edu.sdjzu.xg.kcsj.security.ResourceService;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@WebServlet("/resourceFindAll")
public class ResourceFindAllController extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    JSONObject message = new JSONObject();
    try {
      Collection<Resource> resources = ResourceService.getInstance().findAll();
      message.put("resources", resources);
      response.getWriter().println(message);
    } catch (SQLException e) {
      message.put("message", "数据库操作异常");
      //响应message到前端
      response.getWriter().println(message);
      // 在命令行打印异常信息在程序中出错的位置及原因
      e.printStackTrace();
    } catch (Exception e) {
      message.put("message", "网络异常");
      //响应message到前端
      response.getWriter().println(message);
      // 在命令行打印异常信息在程序中出错的位置及原因
      e.printStackTrace();
    }
  }
}